package org.consoleTesting;

import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class FileLogger {
	private FileWriter	wr;
	private Calendar cal;
	private User user;
	public FileLogger(String file_name, User userForLogging) {
		
		try {
			wr = new FileWriter(file_name,true);
		} catch (IOException e) {
			System.err.println("Can't open file for writing log");
		}
		cal = new GregorianCalendar();
		user = userForLogging;
	}
	
	public void logLogin() {
		try {
			wr.write(String.format("%s %s logged to system %n",getDate(),  user.getName()));
			wr.flush();
		} catch (IOException e) {
			System.err.println("Can't write to log file");
		}
		
	} 
	
	public void logLogout() {
		try {
			wr.write(String.format("%s %s unlogged %n", getDate(), user.getName()));
			wr.flush();
		} catch (IOException e) {
			System.err.println("Can't write to log file");
		}
		
	}
	
	public void logStartTest() {
		try {
			wr.write(String.format("%s $s started test $n", getDate(), user.getName()));
			wr.flush();
		} catch (IOException e) {
			
		}
	}
	
	public void logTestFinished() {
		
		try {
			wr.write(String.format("%s %s finished test", getDate(), user.getName()));
			wr.flush();
		} catch (IOException e) {
			System.err.println("Can't write to log file");
		}
		
	}
	
	private String getDate() {
		return new SimpleDateFormat().format(cal.getTime()) + " ";
	}
}
